package edu.sctu.yueding.utils;

import android.content.Context;

import java.util.HashSet;
import java.util.Set;

import edu.sctu.yueding.config.CacheConfig;
import edu.sctu.yueding.utils.httputils.SharePreference;

public class CacheImgPathUtil {

    private static CacheImgPathUtil cacheImgPathUtil = null;
    private Context context;
    private SharePreference mSharePreferce;


    private CacheImgPathUtil(Context context){
        this.context = context;
    }

    /**
     *单例
     * @param context
     */
    public static CacheImgPathUtil getInstance(Context context){
        if (cacheImgPathUtil == null){
            cacheImgPathUtil = new CacheImgPathUtil(context);
        }
        return cacheImgPathUtil;
    }

    /**
     * set img path(one Set) cache
     * @param selectedImg
     */
    public void setCacheImgPath(Set<String> selectedImg) {
        Set<String> mCacheDefaultSet = new HashSet<String>();
        mSharePreferce = SharePreference.getInstance(context);
        mCacheDefaultSet = mSharePreferce.getSetCache(CacheConfig.CACHE_IMG_PATH_SET_KEY,mCacheDefaultSet);

        if(mCacheDefaultSet.size() == 0){
            mSharePreferce.setSetCache(CacheConfig.CACHE_IMG_PATH_SET_KEY,selectedImg);
        }else {
            for (String str : selectedImg){
                if(!mCacheDefaultSet.contains(str)){
                    mCacheDefaultSet.add(str);
                }
            }
            mSharePreferce.setSetCache(CacheConfig.CACHE_IMG_PATH_SET_KEY,mCacheDefaultSet);
        }
    }

    /**
     * set img path(one String) cache
     * @param selectedImg
     */
    public void setCacheImgPath(String selectedImg) {
        Set<String> mSet = new HashSet<String>();
        Set<String> mCacheSet = new HashSet<String>();

        mSet.add(selectedImg);

        mSharePreferce = SharePreference.getInstance(context);
        mCacheSet = mSharePreferce.getSetCache(CacheConfig.CACHE_IMG_PATH_SET_KEY,mCacheSet);

        if(mCacheSet.size() == 0){
            mSharePreferce.setSetCache(CacheConfig.CACHE_IMG_PATH_SET_KEY,mSet);
        }else {
            if (!mCacheSet.contains(selectedImg)){
                mCacheSet.add(selectedImg);
                mSharePreferce.setSetCache(CacheConfig.CACHE_IMG_PATH_SET_KEY,mCacheSet);
            }
        }
    }


    /**
     * get img path cache
     * @return
     */
    public Set<String> getCacheImgPath() {
        Set<String> cacheImgPath = new HashSet<String>();
        mSharePreferce  = SharePreference.getInstance(context);
        if (mSharePreferce.ifHaveShare(CacheConfig.CACHE_IMG_PATH_SET_KEY))
        cacheImgPath = mSharePreferce.getSetCache(CacheConfig.CACHE_IMG_PATH_SET_KEY,cacheImgPath);
        return cacheImgPath;
    }

    /**
     * 清除path缓存
     */
    public void removePathCache() {
        mSharePreferce = SharePreference.getInstance(context);
        if (mSharePreferce.ifHaveShare(CacheConfig.CACHE_IMG_PATH_SET_KEY)){
            mSharePreferce.removeOneCache(CacheConfig.CACHE_IMG_PATH_SET_KEY);
        }
    }
}
